package com.productivity.control;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.jogamp.opengl.util.FPSAnimator;
import com.productivity.model.Game;
import com.productivity.model.common.Direction;
import com.productivity.model.common.Vector3D;
import com.productivity.view.MyCanvas;

public class Scroll implements KeyListener {
	private Game game;
	private MyCanvas view;
	
	public Scroll() {
		game = new Game();
		view = new MyCanvas(this, game);
		
		SwingUtilities.invokeLater(new Runnable() {
	         @Override
	         public void run() {
	            // Create a animator that drives canvas' display() at the specified FPS.
	            final FPSAnimator animator = new FPSAnimator(view, MyCanvas.FPS, true);
	 
	            // Create the top-level container
	            final JFrame frame = new JFrame(); // Swing's JFrame or AWT's Frame
	            frame.requestFocus();
	            frame.setLocation(200, 650);
	            frame.getContentPane().add(view);
	            
	            frame.addWindowListener(new WindowAdapter() {
	               @Override
	               public void windowClosing(WindowEvent e) {
	                  // Use a dedicate thread to run the stop() to ensure that the
	                  // animator stops before program exits.
	                  new Thread() {
	                     @Override
	                     public void run() {
	                        if (animator.isStarted()) animator.stop();
	                        System.exit(0);
	                     }
	                  }.start();
	               }
	            });
	            frame.setTitle("Scroll");
	            frame.pack();
	            view.requestFocusInWindow();
	            frame.setVisible(true);
	            animator.start(); // start the animation loop
	         }
	      });
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_LEFT)
			game.getPlayer().rotate(3);
		if(e.getKeyCode()==KeyEvent.VK_RIGHT)
			game.getPlayer().rotate(-3);
		if(e.getKeyCode()==KeyEvent.VK_UP)
			game.getPlayer().move();
	
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_LEFT||e.getKeyCode()==KeyEvent.VK_RIGHT)
			game.getPlayer().stopRotate();
		if(e.getKeyCode()==KeyEvent.VK_UP)
			game.getPlayer().stopMove();
	}
	public static void main(String[] args) {
		new Scroll();
	}
}
